1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : ressourcen/classes/archiver/class.zipprovider.inc.php
5 : - Module group: File Manager
6 : - Description: Class "ZipProvider"
7 : - Version: $Id: class.zipprovider.inc.php 2348 2009-06-04 13:29:47Z commana $
8 : - Author(s): Bjoern Kasteleiner <bjoern.kasteleiner@mni.fh-giessen.de>
9 : +---------------------------------------------------------------------------+
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or any later version.
14 : +---------------------------------------------------------------------------+
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 : +--------------------------------------------------------------------------*/
23 :
24 1 : if ( !defined("PATH_TO_ROOT") ) {
25 0 : define( "PATH_TO_ROOT", "../../../" );
26 0 : }
27 :
28 1 : require_once ("class.file.inc.php");
29 1 : require_once ("class.folder.inc.php");
30 1 : require_once ("class.zipvisitor.inc.php");
31 1 : require_once ("interface.archiveprovider.inc.php");
32 1 : require_once ("class.archiverexception.inc.php");
33 :
34 :
35 : /**
36 : * Provides a zip archive of a folder.
37 : *
38 : * @author Bjoern Kasteleiner <bjoern.kasteleiner@mni.fh-giessen.de>
39 : * @version $Id: class.zipprovider.inc.php 2348 2009-06-04 13:29:47Z commana $
40 : */
41 1 : class ZipProvider implements ArchiveProvider {
42 : private $folder;
43 : private $recursion;
44 : private $tempFile;
45 :
46 : /**
47 : * Constructor for ZipProvider class.
48 : *
49 : * Basic checks of incomming values.
50 : * If $tempFile is null, the upload_path is used to
51 : * create a temporary file for zip creation.
52 : *
53 : * @param Folder $folder Folder
54 : * @param Boolean $recursion Recursion flag (Default: False)
55 : * @param String $tempFile Temporary filepath (Default: null)
56 : */
57 : public function __construct(Folder $folder, $recursion = FALSE, $tempFile = null) {
58 0 : assert(!is_null($folder));
59 0 : assert($folder instanceof Folder);
60 :
61 0 : global $settings;
62 :
63 0 : $this->folder = $folder;
64 0 : $this->recursion = $recursion;
65 :
66 0 : if (empty($tempFile)) {
67 0 : $basedir = PATH_TO_ROOT.$settings["upload_path"];
68 0 : $basedir .= 'filemanager/courseID/'.$_SESSION['course'];
69 :
70 0 : $tempFile = tempnam($basedir, "archiver");
71 0 : }
72 :
73 0 : $this->tempFile = $tempFile;
74 0 : }
75 :
76 : /**
77 : * Destructor of ZipProvider.
78 : *
79 : * Used to clean up temporary files if needed.
80 : *
81 : */
82 : public function __destruct() {
83 0 : if (file_exists($this->tempFile)) {
84 0 : unlink($this->tempFile);
85 0 : }
86 0 : }
87 :
88 : /**
89 : * Setter for recursion flag.
90 : *
91 : * @param Boolean $recursion Recursion flag
92 : */
93 : public function setRecursion($recursion) {
94 0 : $this->recursion = $recursion;
95 0 : }
96 :
97 : /**
98 : * Getter for recursion flag.
99 : *
100 : * @return Boolean Recursion flag
101 : */
102 : public function getRecursion() {
103 0 : return $this->recursion;
104 : }
105 :
106 : /**
107 : * Setter for temporary file.
108 : *
109 : * @param String $tempFile Temporary filepath
110 : */
111 : public function setTempFile($tempFile) {
112 0 : $this->tempFile = $tempFile;
113 0 : }
114 :
115 : /**
116 : * Getter for temporary file.
117 : *
118 : * @return String Temporary filepath
119 : */
120 : public function getTempFile() {
121 0 : return $this->tempFile;
122 : }
123 :
124 : /**
125 : * Setter for archiving folder.
126 : *
127 : * @param Folder $folder Folder
128 : */
129 : public function setFolder(Folder $folder) {
130 0 : $this->folder = $folder;
131 0 : }
132 :
133 : /**
134 : * Getter for archiving folder.
135 : *
136 : * @return Folder Folder
137 : */
138 : public function getFolder() {
139 0 : return $this->folder;
140 : }
141 :
142 : /**
143 : * Create an archive of the given Folder.
144 : *
145 : * With or without subfolders, depending on recursion flag.
146 : */
147 : public function createArchive() {
148 0 : $zipArchiver = new ZipArchive();
149 0 : if ($zipArchiver->open($this->tempFile, ZipArchive::OVERWRITE) !== TRUE) {
150 0 : throw new ArchiverException("Fehler beim Anlegen des Archivs. ".$this->tempFile);
151 : }
152 :
153 0 : $zipVisitor = new ZipVisitor($zipArchiver, $this->recursion);
154 0 : $this->folder->archiverAccept($zipVisitor);
155 :
156 0 : if ($zipArchiver->status != ZipArchive::ER_OK) {
157 0 : throw new ArchiverException("Fehler beim Erzeugen des Archivs: ".$zipArchiver->status);
158 : }
159 0 : $zipArchiver->close();
160 0 : }
161 :
162 : /**
163 : * Send archive to the client.
164 : *
165 : * Send needed header informations and logging folder
166 : * download and increment folder download count.
167 : */
168 : public function sendArchive() {
169 0 : global $resDB;
170 :
171 0 : if (file_exists($this->tempFile)) {
172 0 : $filename = $resDB->getCourseShortNameByID($this->folder->getCourseID());
173 0 : $filename .= "-".$this->folder->getLink().".zip";
174 :
175 0 : $this->logFolderDownload($this->folder);
176 :
177 0 : header("Content-Type: application/zip");
178 0 : header("Content-Disposition: attachment; filename=\"".$filename."\"");
179 0 : header("Content-Transfer-Encoding: binary");
180 0 : header("Content-Length: ".filesize($this->tempFile));
181 :
182 0 : readfile($this->tempFile);
183 0 : unlink($this->tempFile);
184 0 : } else {
185 0 : throw new ArchiverException("Bereitgestelltes Zip Archive konnte nicht gefunden werden.");
186 : }
187 0 : }
188 :
189 : /**
190 : * Logging folder download and increment folder download count.
191 : *
192 : * @param Folder $folder Folder
193 : */
194 : private function logFolderDownload(Folder $folder) {
195 0 : global $resDB;
196 :
197 0 : $resDB->logResourceDownload($folder->getID());
198 :
199 0 : $files = $folder->getFiles();
200 0 : foreach ($files as $file) {
201 0 : $resDB->logResourceDownload($file->getID());
202 0 : }
203 :
204 0 : if ($this->recursion) {
205 0 : $subFolders = $folder->getSubFolders();
206 0 : foreach ($subFolders as $subFolder) {
207 0 : $this->logFolderDownload($subFolder);
208 0 : }
209 0 : }
210 0 : }
211 :
212 :
213 : /**
214 : * Check archive filesizes.
215 : *
216 : * Calculate the filesize of the folder and its subfolders.
217 : *
218 : * @param Folder $folder Folder
219 : * @param Boolean $recursion Recursion flag
220 : * @return Integer Size of folder and subfolders
221 : */
222 : public static function checkArchiveSize(Folder $folder, $recursion) {
223 0 : global $resDB;
224 :
225 0 : $size = 0;
226 :
227 0 : $files = $folder->getFiles();
228 0 : foreach ($files as $file) {
229 0 : $size += $file->getSize();
230 0 : }
231 :
232 0 : if ($recursion) {
233 0 : $subFolders = $folder->getSubFolders();
234 0 : foreach ($subFolders as $subFolder) {
235 0 : $size += self::checkArchiveSize($subFolder, $recursion);
236 0 : }
237 0 : }
238 :
239 0 : return $size;
240 : }
241 : }
242 :
|